package com.office.automate.dataaccess.impl.user;

import java.util.List;
import java.util.Properties;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.office.automate.configuration.property.ConfigurationProperties;
import com.office.automate.dataaccess.def.user.EmployeeQueryDao;
import com.office.automate.dataaccess.framework.DataAccessSessionFactory;
import com.office.automate.exception.administration.SecurityLevelExceptionKeys;
import com.office.automate.exception.framework.FrontierVisibleException;
import com.office.automate.model.employee.Employee;

public class EmployeeQueryDaoImplementer implements EmployeeQueryDao {
	private Properties configProps = ConfigurationProperties.getProps(); 
	private static SessionFactory sessionFactory = null;
	private Session session = null;

	static {
		sessionFactory = DataAccessSessionFactory.getSessionFactory();
	}

	public EmployeeQueryDaoImplementer() {
		session = sessionFactory.openSession();		
	}

	@SuppressWarnings("unchecked")
	public List<Employee> getManagersOrSupervisors() throws FrontierVisibleException {
		String roleIds = configProps.getProperty("COMPANY_MANAGER_SUPERVISOR_DESIGNATION_CODES");
		
		List<Employee> managersOrSupervisors = null;
		try {
			 String SQL_QUERY ="from Employee employee where employee.designationCode in (" + roleIds + ")";
			 Query queryResult = session.createQuery(SQL_QUERY);
			 managersOrSupervisors = queryResult.list();
		} catch(Exception e) {
			throw new FrontierVisibleException(SecurityLevelExceptionKeys.ERROR_OCCORRED_DURING_RETRIEVAL, e);
		} finally {
			session.flush();
			session.close();
		}
		return managersOrSupervisors;
	}

	@SuppressWarnings("unchecked")
	public List<Employee> getAllEmployees() throws FrontierVisibleException {
		List<Employee> allEmployees = null;
		try {
			 String SQL_QUERY ="from Employee employee where employee.username <> 'administrator'";
			 Query queryResult = session.createQuery(SQL_QUERY);
			 allEmployees = queryResult.list();
		} catch(Exception e) {
			throw new FrontierVisibleException(SecurityLevelExceptionKeys.ERROR_OCCORRED_DURING_RETRIEVAL, e);
		} finally {
			session.flush();
			session.close();
		}
		return allEmployees;
	}

}
